package com.indusfo.spc.service.impl;

import com.indusfo.spc.exception.GlobalException;
import com.indusfo.spc.exception.ModifyFailedException;
import com.indusfo.spc.exception.ParamsErrorException;
import com.indusfo.spc.mapper.OrderBomMapper;
import com.indusfo.spc.pojo.OrderBadpheno;
import com.indusfo.spc.pojo.OrderBom;
import com.indusfo.spc.service.OrderBomService;
import com.indusfo.spc.vo.HintMessage;
import com.indusfo.spc.vo.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author liub
 * @Date 2019/11/26 10:48
 * @Version 1.0
 */

@Service
public class OrderBomServiceImpl implements OrderBomService {

    /**
     * 声明日志的静态变量
     */
    private static final Logger logger = LoggerFactory.getLogger(OrderBomServiceImpl.class);

    @Resource
    private OrderBomMapper orderBomMapper;

    /**
     * 查询
     */
    @Override
    public JSONObject queryOrderBom(OrderBom orderBom) {
        try {
            //获取页面的数据个数
            Integer pagesize = orderBom.getPagesize();
            //获取查询的页数
            Integer pageindex = orderBom.getPageindex();
            if (pagesize != null && pageindex != null) {
                orderBom.setIncept(pagesize * (pageindex - 1));
            }
            //执行查询操作
            List<OrderBom> orderBomList =new ArrayList<>();
            if(orderBom.getOrderFlowId()==null){
                return JSONObject.oK(HintMessage.QUERY_NULL, orderBomList, 0);
            }
              orderBomList = orderBomMapper.queryOrderBom(orderBom);

            //如果页数和页面数量都不为空的情况下进行计数
            if (orderBomList.isEmpty()) {
                return JSONObject.oK(HintMessage.QUERY_NULL, orderBomList, 0);
            } else {
                Integer count = orderBomMapper.countOrderBom(orderBom);
                return JSONObject.oK(HintMessage.QUERY_SUCCESS, orderBomList, count);
            }

        } catch (GlobalException e) {
            logger.error(e.getMessage(), e);
            return JSONObject.build(500, e.getMessage());

        }
    }

    /**
     * 新增
     */
    @Override
    public JSONObject insertOrderBom(OrderBom orderBom) {
        try {
            if (orderBom.getOrderBomId() != null) {
                throw new ModifyFailedException(HintMessage.INSERT_ID);
            }
            //初始化基础数据
            if (orderBom.getDataState() == null) {
                orderBom.setDataState(1);
            }
            //判重
            checkParam(orderBom);
            //执行新增操作
            Integer row = orderBomMapper.insertOrderBom(orderBom);
            if (row == 0) {
                throw new ModifyFailedException(HintMessage.INSERT_DEFEATED);
            }
            return JSONObject.oK(HintMessage.INSERT_SUCCESS);
        } catch (GlobalException e) {
            logger.error(e.getMessage());
            return JSONObject.build(500, e.getMessage());
        }

    }

    /**
     * 修改
     */
    @Override
    public JSONObject updateOrderBom(OrderBom orderBom) {
        try {
            if (orderBom.getOrderBomId() == null) {
                throw new ParamsErrorException(HintMessage.UPDATE_ID);
            }
            //初始化基础数据
            if (orderBom.getDataState() == null) {
                orderBom.setDataState(1);
            }
            //判重
            checkParam(orderBom);
            //执行修改操作
            int row = orderBomMapper.updateOrderBom(orderBom);
            if (row == 0) {
                throw new ModifyFailedException(HintMessage.UPDATE_DEFEATED);
            }
            return JSONObject.oK(HintMessage.UPDATE_SUCCESS);
        } catch (GlobalException e) {//捕获自定义异常
            logger.error(e.getMessage());
            return JSONObject.build(500, e.getMessage());
        }
    }

    /**
     * 删除
     */
    @Override
    public JSONObject deleteOrderBom(List<Integer> orderBomIds, Integer dataState) {
        try {
            //空值判断
            if (dataState == null) {
                throw new ParamsErrorException(HintMessage.DATASTATE_NULL);
            }
            if (orderBomIds == null) {
                throw new ParamsErrorException(HintMessage.LIST_NULL);
            }
            //记录异常信息
            String msg = "";
            switch (dataState) {
                case 1:
                    msg = HintMessage.START_VALUE;
                    break;
                case 2:
                    msg = HintMessage.DELETE_VALUE;
                    break;
                case 3:
                    msg = HintMessage.STOP_VALUE;
                    break;
                default:
                    throw new ParamsErrorException(HintMessage.VIOLATION_OPERATION);
            }
            //执行删除操作
            int row = orderBomMapper.deleteOrderBom(orderBomIds, dataState);
            //声明msg字符串，用来存抛出的异常
            if (row == 0) {
                msg = msg + HintMessage.DEFEATED_VALUE;
            } else {
                msg = msg + HintMessage.SUCCESS_VALUE;
            }
            return JSONObject.oK(msg);
        } catch (GlobalException e) {
            logger.error(e.getMessage());
            return JSONObject.build(500, e.getMessage());
        }
    }

    /**
     * 判重
     */
    public void checkParam(OrderBom orderBom) {

        //任务单流程ID
        Integer orderFlowId = orderBom.getOrderFlowId();
        //物料ID
        Integer matId = orderBom.getMatId();
        //扣减方式ID
        Integer reduceTypeId = orderBom.getReduceTypeId();
        //是否为关键物料
        Integer keyMaterial = orderBom.getKeyMaterial();

        //任务单流程ID
        if (orderFlowId == null) {
            throw new ParamsErrorException(HintMessage.ORDERFLOWID_NULL);
        }
        //物料ID
        if (matId == null) {
            throw new ParamsErrorException(HintMessage.MATID_NULL);
        }
        //扣减方式ID
        if (reduceTypeId == null) {
            throw new ParamsErrorException(HintMessage.REDUCETYPEID_NULL);
        }
        //是否为关键物料
        if (keyMaterial == null) {
            throw new ParamsErrorException(HintMessage.KEYMATERIAL_NULL);
        }
        // 执行判重操作
        Integer Counts = orderBomMapper.checkOrderBom(orderBom);
        if (Counts != 0) {
            throw new ParamsErrorException(HintMessage.REPEATING_DATA);
        }

    }
}
